﻿using BW.EatRabbit.API.Application.Commands.Processing;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;
using System.Collections.Generic;

namespace BW.EatRabbit.API.Application.CommandHandlers.Processing
{
    public class GetProcessingQueryCommandHandler : IRequestHandler<GetProcessingQueryCommand, APIResult<Processing_Information>>
    {
        private readonly IBaseRepository<Processing_Information> _processingRepository;
        public GetProcessingQueryCommandHandler(IBaseRepository<Processing_Information> processingRepository)
        {
            _processingRepository = processingRepository;
        }
        public Task<APIResult<Processing_Information>> Handle(GetProcessingQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<Processing_Information> result = new APIResult<Processing_Information>();

            var ment = _processingRepository.GetAll().FirstOrDefault(x => x.ProductionBatch == request.ProductionBatch);
            if (ment == null)
            {
                result.Code = ResultCode.查询失败;
                result.Message = ResultCode.查询失败.ToString();

            }
            else
            {
                result.Code = ResultCode.查询成功;
                result.Message = ResultCode.查询成功.ToString();
                result.Data = ment;
            }
            return Task.FromResult(result);
        }
    }
}
